package com.wanmait.spacesys.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * 监听器
 *
 */
@WebListener
public class OnlineListener implements HttpSessionListener, ServletContextListener {

    /**
     * Default constructor. 
     */
    public OnlineListener() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * session创建
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent se)  { 
    	 //获得session对象 
         HttpSession session=se.getSession();
         //获得application对象
         ServletContext  application= session.getServletContext();
         int count=(int)application.getAttribute("onlineCount");
        
         //count+1
         count++;
         application.setAttribute("onlineCount", count);
         session.setAttribute("onlineCount", count);
    }

	/**
	 * session销毁
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent se)  { 
         // TODO Auto-generated method stub
    }

	/**
	 * application销毁
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent sce)  { 
         // TODO Auto-generated method stub
    }

	/**
	 * application初始化
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent sce)  { 
    		//获得application对象
    	ServletContext application= sce.getServletContext();
    	application.setAttribute("onlineCount", 0);
    }
	
}
